package messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

import GUI.LoginGUI;

import ocsf.server.ConnectionToClient;
import controllers.DBConnection;
import controllers.DBController;
import controllers.WorkerController;
import controllers.CustomerController;
import entity.User;
import entity.UserLoginInfo;
import controllers.EchoServer;


/**
 * this class handle with the login message from DB 
 * checking if user exist 
 * if the password is correct and match
 * setting user data
 * update DB of user connection
 */
public class LoginMessage 
{
	String[] userArr;
	ConnectionToClient client;
	WorkerController workerC;
	CustomerController customerC;
	UserLoginInfo loginInfo;
	//EchoServer echo;
	
	User user;
	private String userID;
	private String password;
	private int status;
	private int loginResult = -9;
	private int permmision = 0 ;
	private String Fname ; 
	private String Lname;
	private String email;
	private String phone;
	private int numOfComplaints;
	private int numOfSearches;
	ConnectionToClient client1=new ConnectionToClient();

	public LoginMessage() {}
	
	public LoginMessage(String[] user, ConnectionToClient client)
	{
		this.userArr=user;
		this.client=client;	
		
	}
	
	
	public LoginMessage(String[] user)
	{
		this.userArr=user;
		this.client=client;	
		
	}
	
	
	public void activation(){
		
		System.out.println("login message : "+userArr[0]+" "+userArr[1]+" "+userArr[2]+" "+userArr[3]+" "+userArr[4]);
		int port=Integer.valueOf(userArr[4]);
		loginInfo=new UserLoginInfo(userArr[1],userArr[2],userArr[3],port); 
		/*try {
			client1.sendToClient("check_check");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		
		try{
					
			DBConnection conn=new DBConnection();
			java.sql.PreparedStatement pr1= conn.getDBConnection().prepareStatement("SELECT * " + "FROM sgi_system.users " + "WHERE ID ='" + userArr[1]+"' AND Password='" + userArr[2]+"';");
			ResultSet rs=pr1.executeQuery();			
		
			if(!rs.next()){
				pr1.close();
				rs.close();
				System.out.println("No match between id and password that inserted in DB");
				JOptionPane.showMessageDialog(null, "No match between id and password");
			}
			/**
			 *  No match id and password
			 */
			  
			 
			else{
				
					
				    /**
				     * setting user data 		 	
				     */
				 	this.setStatus(rs.getInt("status"));
				 	 this.setID(rs.getString("ID"));
                     this.setPass(rs.getString("Password"));
                     this.setLoginPermmision(rs.getInt("Permmision"));
                     this.setEmail(rs.getString("email"));
                     this.setFname(rs.getString("FirstName"));
                     this.setLname(rs.getString("LastName"));
                     this.setPhone(rs.getString("PhoneNumber"));
                     this.setNumOfComplaints(rs.getInt("numOfComplaints"));
                     this.setNumOfSearches(rs.getInt("numOfSearches"));
                      System.out.println(" LoginMessage  status=  "+this.status+ "  " );

                     if( 1==(Integer.parseInt(rs.getString("status") ))) {
         				pr1.close();
         				rs.close();
         				System.out.println("user with this id already loged");
         				JOptionPane.showMessageDialog(null, "user with this id already loged");
         				exit(0);
         				}
                  
                     /**
                      * user already connected
                      */
                     
                     else{
                     /**
                      * user info as written in DB
                      */
                    	 pr1.close();
                    	 rs.close();
                    user=new User(userID,password, Fname,Lname,email,phone,permmision,status);
                     user.printUser();
                  
                     /**
                      * update DB of user connection	
                      */
                     DBConnection conn2= new DBConnection();
                     java.sql.PreparedStatement pr2 = conn2.getDBConnection().prepareStatement( "Update sgi_system.users SET status=? WHERE ID=? ");                
                     pr2.setString(1,"1");
                     pr2.setString(2, userID);
         		     pr2.executeUpdate();	
                     pr2.close();
         			
                     
                     
               
							if(this.permmision==1 ){
								customerC = new CustomerController(user);
								customerC.activateCustomerGUI();
							}
		                     else {
		                    	 workerC = new WorkerController(user);
		                    	 workerC.activateGUI();
		                     }
                    	 
                     }
			}
		}catch (Exception e)
	       {
	    	   e.printStackTrace();
	    	   System.out.println("ERROR in DB");
	       
	       }
		
	}

	
	
	private void exit(int i) {
		// TODO Auto-generated method stub
		
	}


	public User getUser(){
		return this.user;
	}
	
	public String getID()
	{
		return this.userID;
	}
	public void setID(String id)
	{
		 this.userID = id;
	}

	public String getPassword()
	{
		return this.password;
	}
	
	public void setPass(String pass)
	{
		 this.password = pass;
	}
	
    public int getLoginResult()
    {
    	return this.loginResult;
    }
	
    public void setLoginPermmision(int permmision)
    {
    	this.permmision=permmision; 
    }
	
    public int getLoginPermmision()
    {
    	return this.permmision;
    }
	
    public void setLoginResult(int result)
    {
    	this.loginResult = result ; 
    }

	public String getFname() 
	{
		return this.Fname;
	}

	public void setFname(String fname) 
	{
		this.Fname = fname;
	}

	public String getLname()
	{
		return this.Lname;
	}

	public void setLname(String lname) 
	{
		this.Lname = lname;
	}

	public int getStatus()
	{
		return this.status;
	}

	public void setStatus(int status)
	{
		this.status = status;
		
	}


	public String getEmail()
	{
		return this.email;
	}

	public void setEmail(String email) 
	{
		this.email = email;
	}

	public String getPhone()
	{
		return this.phone;
	}

	public void setPhone(String phone) 
	{
		this.phone = phone;
	}
    
	public ConnectionToClient getClient()
	{
		return this.client;
	}

	public int getNumOfSearches() 
	{
		return this.numOfSearches;
	}


	public void setNumOfSearches(int numOfSearches) 
	{
		this.numOfSearches = numOfSearches;
	}

	
	public int getNumOfComplaints() 
	{
		return this.numOfComplaints;
	}


	public void setNumOfComplaints(int numOfComplaints) 
	{
		this.numOfComplaints = numOfComplaints;
	}
	
}
	

